package cn.iocoder.yudao.module.scrm.service.schedules;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesTeacherPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesTeacherSaveReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.PersonInfo;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedulesteacher.SchedulesTeacherDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedulesteacher.SchedulesTeacherMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.SCHEDULES_TEACHER_NOT_EXISTS;


/**
 * 排期老师 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class SchedulesTeacherServiceImpl implements SchedulesTeacherService {

    @Resource
    private SchedulesTeacherMapper schedulesTeacherMapper;

    @Override
    public Boolean createSchedulesTeacher(Long schedulesId, List<PersonInfo> teacherInfos) {
        List<SchedulesTeacherDO> schedulesTeachers = convertToSchedulesTeacherDOList(schedulesId, teacherInfos);
        // 插入
        return schedulesTeacherMapper.insertOrUpdateBatch(schedulesTeachers);
    }

    @Override
    public void updateSchedulesTeacher(SchedulesTeacherSaveReqVO updateReqVO) {
        // 校验存在
        validateSchedulesTeacherExists(updateReqVO.getId());
        // 更新
        SchedulesTeacherDO updateObj = BeanUtils.toBean(updateReqVO, SchedulesTeacherDO.class);
        schedulesTeacherMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchedulesTeacher(Long id) {
        // 校验存在
        validateSchedulesTeacherExists(id);
        // 删除
        schedulesTeacherMapper.deleteById(id);
    }

    private void validateSchedulesTeacherExists(Long id) {
        if (schedulesTeacherMapper.selectById(id) == null) {
            throw exception(SCHEDULES_TEACHER_NOT_EXISTS);
        }
    }

    @Override
    public SchedulesTeacherDO getSchedulesTeacher(Long id) {
        return schedulesTeacherMapper.selectById(id);
    }

    @Override
    public PageResult<SchedulesTeacherDO> getSchedulesTeacherPage(SchedulesTeacherPageReqVO pageReqVO) {
        return schedulesTeacherMapper.selectPage(pageReqVO);
    }

    private List<SchedulesTeacherDO> convertToSchedulesTeacherDOList(Long schedulesId, List<PersonInfo> teacherInfos) {
        return teacherInfos.stream()
                .map(Optional::ofNullable)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .filter(teacherInfo -> teacherInfo.getId() != null && teacherInfo.getName() != null)
                .map(teacherInfo -> {
                    SchedulesTeacherDO schedulesTeacherDO = new SchedulesTeacherDO();
                    schedulesTeacherDO.setSchedulesId(schedulesId);
                    schedulesTeacherDO.setTeacherId(teacherInfo.getId());
                    schedulesTeacherDO.setTeacherName(teacherInfo.getName());
                    return schedulesTeacherDO;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<SchedulesTeacherDO> getTeachersBySchedulesIds(List<Long> schedulesIds) {
        return schedulesTeacherMapper.selectList(SchedulesTeacherDO::getSchedulesId, schedulesIds);
    }

}